const express = require("express")
const router = express.Router()
const personalDB = require("../../../db/personal")
const messageDB = require("../../../db/message")

// 个人档
router.get("/getInfo",async(req,res)=>{
  try {
    let doc = await personalDB.findOne({})
    res.send({
      code:0,
      mes:"个人档信息",
      data:doc
    })
  } catch (error) {
    res.send({
      code:5,
      mes:"服务器异常，请稍后再试"
    })
  }
})

// 访客留言
router.post("/Guestbook",async(req,res)=>{
  try {
    let {name,email,phone,message} = req.body.data
    if(!name.trim()){
      return res.send({
        code:23,
        mes:"用户名不符合规范"
      })
    }
    if(!email.trim()){
      return res.send({
        code:24,
        mes:"email不符合规范"
      })
    }
    if(!phone.trim()){
      return res.send({
        code:23,
        mes:"phone不符合规范"
      })
    }
    if(!message.trim()){
      return res.send({
        code:23,
        mes:"message不符合规范"
      })
    }
    await messageDB.create({
      name,email,phone,message
    })
    res.send({
      code:0,
      mes:"留言成功，感谢您的留言"
    })
  } catch (error) {
    console.log(error)
    res.send({
      code:5,
      mes:"服务器异常，请稍后再试"
    })
  }
})

module.exports = router